package com.entrogy.controller;


import com.entrogy.bean.DepartmentEntity;
import com.entrogy.bean.PageBean;
import com.entrogy.bean.PositionEntity;
import com.entrogy.bean.UserEntity;
import com.entrogy.config.Result;
import com.entrogy.service.DepartmentService;
import com.entrogy.utils.ApiReturnUtil;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 部门的相关操作
 * @Author xxx
 * @Date 2019-2
 */

@Api(description = "部门的操作接口")
@RestController
@RequestMapping(value = "/department")
public class DepartmentController  {

    private Logger logger = LoggerFactory.getLogger(DepartmentController.class);

    @Autowired
    private DepartmentService departmentService;

    @ApiOperation(value = "添加一个对应的部门")
    @RequestMapping(value = "/insertDepartment",method = RequestMethod.POST)
//    @RequiresPermissions("department:add")
    public Map<String,Object> insertDepartment(@ModelAttribute(value="departmentEntity") DepartmentEntity departmentEntity){
        UserEntity currentUser = (UserEntity) SecurityUtils.getSubject().getSession().getAttribute("currentUser");
        departmentEntity.setSchool_code(currentUser.getSchoolCode());
        // 新增部门前判断部门名称是否已经存在
        Map<String, Object> params = new HashMap<>();
        params.put("school_code", currentUser.getSchoolCode());
        List<Map<String, Object>> departmentList = departmentService.selectDepartmentByT(params);
        for (Map<String, Object> department : departmentList) {
            if (departmentEntity.getDepartment_name().equals(department.get("department_name"))){
                return ApiReturnUtil.error("对不起，新增部门失败，该部门已经存在！");
            }
        }
        int code = departmentService.insertDepartment(departmentEntity);
        if(code == 1){
            return ApiReturnUtil.success("新增部门成功",null);
        }else {
            return ApiReturnUtil.error("新增部门失败");
        }
   }

    @ApiOperation(value = "根据部门编码修改其对应的部门信息")
    @RequestMapping(value = "updateDepartment",method = RequestMethod.POST)
//    @RequiresPermissions("department:update")
    public Map<String,Object> updateDepartment(@ModelAttribute(value="departmentEntity") DepartmentEntity departmentEntity){
        int code = departmentService.updateDepartment(departmentEntity);
        if(code == 1){
            return ApiReturnUtil.success("修改成功",null);
        }else {
            return ApiReturnUtil.error("对不起,网络异常请稍后重试.");
        }
    }

    @ApiOperation(value = "根据部门编号删除其对应的部门")
    @RequestMapping(value = "deleteDepartment",method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "department_id", value = "部门编码", required = true, dataType = "Integer", paramType = "query")
    })
//    @RequiresPermissions("department:delete")
    public Map<String,Object> delete(@RequestParam("department_id")Integer department_id){
        int code = departmentService.deleteDepartment(department_id);
        if(code == 1){
            return ApiReturnUtil.success("删除成功",null);
        }else {
            return ApiReturnUtil.error("对不起,网络异常请稍后重试.");
        }
    }


    @ApiOperation(value = "调用User表中school_id")
    @RequestMapping(value = "getSchoolId",method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "department_id", value = "部门编码", required = true, dataType = "Integer", paramType = "query")
    })
    public Map<String,Object> getSchoolId(Integer did){int code = departmentService.getSchoolId(did);
        if(code == 1){
            return ApiReturnUtil.success("调用成功",null);
        }else {
            return ApiReturnUtil.error("对不起,网络异常请稍后重试.");
        }
    }

    /**
     * 获取部门列表
     * @param
     * @param
     * @return
     */
    @RequestMapping(value = "getDepartmentList",method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "department_name", value = "部门名称", required = false, dataType = "String", paramType = "query")
    })
//    @RequiresPermissions("department:list")
    public List<Map<String,Object>> getDepartmentList( String department_name,@RequestParam("schoolCode") String schoolCode){
        Map<String,Object> map=new HashMap<>();
        //获取session 中的用户实体
        map.put("school_code",schoolCode);
        if(department_name!=null && !"".equals(department_name)){
            map.put("department_name",department_name);
        }
        List<Map<String,Object>> list=departmentService.selectDepartmentByT(map);
        return list;
    }

    /**
     * 查询分页信息
     * @param schoolCode
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping(value="getPositionList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "schoolCode", value = "学校id", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "did", value = "部门id", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "positionName", value = "职位名称", required = false, dataType = "String", paramType = "query")
    })
    public Result<PositionEntity>  getPositionList(Integer schoolCode, Integer currentPage, Integer pageSize, String positionName, Integer did){
        Map<String,Object> map = new HashMap<>();
        if (StringUtils.isNoneBlank(schoolCode.toString())){
            map.put("school_code", schoolCode);
        }
        if (StringUtils.isNoneBlank(positionName)){
            map.put("positionName", positionName);
        }
        if (did != null){
            if (StringUtils.isNoneBlank(did.toString())){
                map.put("did", did);
            }
        }
        int total1 = departmentService.getTotalPositionList(map);
        PageHelper.startPage(currentPage, pageSize);
        List<PositionEntity> list = departmentService.getPositionList(map);
        return  new Result<PositionEntity>().getResult(401,"查询成功",1,new PageBean<PositionEntity>().pageBeans(list, total1));
    }

    /**
     * 修改名称
     * @param positionEntity
     * @return
     */
    @PostMapping(value="updatePositionById")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "schoolCode", value = "部门名称", required = false, dataType = "Integer", paramType = "query")*/
    })
   public  Result   updatePositionById(PositionEntity positionEntity){

        int  code=departmentService.updatePositionById(positionEntity);
        if (code==1){
        return  new Result().getResult(401,"更新成功",1,"");
        }
        return  new Result().getResult(402,"更新失败",2,"");
   }
}


